﻿using Model;
using System;

namespace Model.Extend
{
    public static class ModelAttribute
    {
        public static TableAttribute GetTableAttribute<T>()
        {
            Type type = typeof(T);
            object[] objs = type.GetCustomAttributes(typeof(TableAttribute), true);

            TableAttribute ta = new TableAttribute("");
            if (objs.Length <= 0)
            {
                ta = new TableAttribute(typeof(T).Name, "ID");
                //throw new Exception("实体类没有标识TableAttribute属性");
            }
            else
            {
                object obj = objs[0];
                ta = (TableAttribute)obj;
            }
            return ta;
        }
        /// <summary>
        /// 获取实体表名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetTableName<T>()
        {
            TableAttribute ta = GetTableAttribute<T>();
            return ta.TableName;//获取表名称
        }
        public static string GetPKName<T>()
        {
            TableAttribute ta = GetTableAttribute<T>();
            return ta.PKName;//获取表名称
        }
        /// <summary>
        /// 通过实体类字段获取实体字段名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="m"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static string GetFieldName<T>(this string propertyName)
        {
            //FieldAttribute fieldAttribute = m.GetFieldAttribute(propertyName);
            System.Reflection.PropertyInfo field = typeof(T).GetProperty(propertyName);
            if (field == null)
            {
                throw new Exception("属性名" + propertyName + "不存在");
            }
            object[] objs = field.GetCustomAttributes(typeof(FieldAttribute), true);
            if (objs.Length <= 0)
            {
                throw new Exception("类体属性名" + propertyName + "没有标识FieldAttribute属性");
            }
            else
            {
                object obj = objs[0];
                FieldAttribute fieldAttribute = (FieldAttribute)obj;
                //fieldAttribute.FieldValue = field.GetValue(this, null);
                //return fieldAttribute;
                return fieldAttribute.FieldName;
            }
        }
        /// <summary>
        /// 通过实体类字段获取实体字段属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyName"></param>
        /// <param name="ShowException">报错时是否返回错误信息并终止,不报错时返回一个字段属性FieldAttribute对象</param>
        /// <returns></returns>
        public static FieldAttribute GetFieldAttribute<T>(this string propertyName, bool ShowException = true)
        {
            System.Reflection.PropertyInfo field = typeof(T).GetProperty(propertyName);
            if (field == null)
            {
                if (ShowException)
                    throw new Exception("属性名" + propertyName + "不存在");
                else
                    return new FieldAttribute();
            }
            object[] objs = field.GetCustomAttributes(typeof(FieldAttribute), true);
            if (objs.Length <= 0)
            {
                if (ShowException)
                    throw new Exception("类体属性名" + propertyName + "没有标识FieldAttribute属性");
                else
                    return new FieldAttribute();
            }
            else
            {
                object obj = objs[0];
                FieldAttribute fieldAttribute = (FieldAttribute)obj;
                //fieldAttribute.FieldValue = field.GetValue(this, null);
                //return fieldAttribute;
                return fieldAttribute;
            }
        }
    }
}
